public class J092702 {
      public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
    }
    public static TreeNode buildTree(int[] preorder, int[] inorder) {
        int len = preorder.length;
        return build(preorder, inorder, 0, len - 1, 0, len - 1);
    }
    public static TreeNode build(int[] preorder, int[] inorder, int left1, int right1, int left2, int right2){
        if(left2 == right2){
            return new TreeNode(inorder[left2]);
        }
        TreeNode root = new TreeNode(preorder[left1]);
        root.left = null;
        root.right = null;
        int key = findNum(inorder, root.val);
        int leftNums = key - left2;
        if(left2 != key){
            root.left = build(preorder, inorder, left1 + 1,left1 + leftNums,  left2, key - 1);
        }
        if(right2 != key){
            root.right = build(preorder, inorder, left1 + leftNums + 1, right1, key + 1, right2);
        }
        return root;
    }
    public static int findNum(int[] nums, int key){
        int i = 0;
        while(i < nums.length){
            if(nums[i] == key){
                break;
            }
            i++;
        }
        return i;
    }

    public static void main(String[] args) {
        int[] inorder = new int[]{1,2,3};
        int[] preorder = new int[]{3,2,1};
        TreeNode root = buildTree(inorder, preorder);
        System.out.println(123);
    }
}
